package game.breeze.resource.manager;

import game.breeze.resource.GameMapDef;

import java.io.File;
import java.io.FileFilter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

public class GameMapResourceManager extends ResourceManager {
	private static final Logger logger = Logger.getLogger(GameMapResourceManager.class);

	public GameMapResourceManager(String resource) {
		super(resource);
	}

	private Map<Integer, GameMapDef> mapDictionary = new HashMap<Integer, GameMapDef>();

	/**
	 * @return the mapDictionary
	 */
	public Map<Integer, GameMapDef> getMapDictionary() {
		return Collections.unmodifiableMap(mapDictionary);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if (resource.isDirectory()) {
			File[] maps = resource.listFiles(new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					return pathname.getName().toLowerCase().endsWith(".xml");
				}
			});
			for (File map : maps) {
				SAXReader reader = new SAXReader();
				Document doc = reader.read(map);
				GameMapDef mapDef = new GameMapDef(doc);
				mapDictionary.put(mapDef.getId(), mapDef);
			}
		} else {
			logger.error("地图数据配置路径错误,地图数据只能为目录.");
		}
	}
}
